package com.jeecms.wicool.dao.activities.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.activities.ActivitiesApplyDao;
import com.jeecms.wicool.entity.activities.ActivitiesApply;

public class ActivitiesApplyDaoImpl extends HibernateBaseDao<ActivitiesApply, Integer> implements ActivitiesApplyDao {

	@Override
	protected Class<ActivitiesApply> getEntityClass() {
		// TODO Auto-generated method stub
		return ActivitiesApply.class;
	}

	@Override
	public Pagination getPage(Integer psid,String name, String phoneNumber, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		Finder f = Finder.create("select bean from ActivitiesApply bean where 1=1 and bean.activities.sid = :psid ");
		f.setParam("psid", psid);
		
		if (!StringUtils.isBlank(name)){
			f.append(" and bean.name like :name");
			f.setParam("name", "%" + name + "%");
		}
		if(phoneNumber!=null){
			f.append(" and bean.phoneNumber like :phoneNumber");
			f.setParam("phoneNumber", "%" + phoneNumber + "%");
		}
		
		f.append(" order by bean.submitTime desc");
		return find(f, pageNo, pageSize);
	}
	@Override
	public List<ActivitiesApply> findByPsid(Integer psid){
		Finder f = Finder.create("select bean from ActivitiesApply bean where 1=1 and bean.activities.sid = :psid ");
		f.setParam("psid", psid);
		f.append(" order by bean.submitTime desc");
		return find(f);
	}
	@Override
	public List<ActivitiesApply> findByUserId(Integer psid,String userId){
		// TODO Auto-generated method stub
				Finder f = Finder.create("select bean from ActivitiesApply bean where 1=1  ");
				if (!StringUtils.isBlank(userId)){
					f.append(" and bean.userId = :userId");
					f.setParam("userId", userId);
				}
				if (null!=psid){
					f.append(" and bean.activities.sid = :psid");
					f.setParam("psid", psid);
				}
				return find(f);
	}
	@Override
	public ActivitiesApply save(ActivitiesApply bean){
		getSession().save(bean);
		return bean;
	}
}
